package com.demo.json;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.demo.util.JacksonJsonUtils;
import com.fasterxml.jackson.core.type.TypeReference;

@MappedTypes({List.class,Map.class,Object.class})
@MappedJdbcTypes({JdbcType.VARCHAR,JdbcType.LONGVARCHAR,JdbcType.OTHER,JdbcType.BLOB})
public class JsonTypeHandler<T> extends AbstractJsonTypeHandler<T> {


    public JsonTypeHandler() {}

    @Override
    protected T parse(String json) {

        TypeReference<T> valueTypeRef = new TypeReference<T>() {};
        return JacksonJsonUtils.readObject(json, valueTypeRef);

    }

    @Override
    protected String toJson(T obj) {
        return JacksonJsonUtils.writeObject(obj);
    }

}
